import request from '@/utils/request';
import { type App, type AppQueryParams, type AppRequest, type PageResult } from '../../types/app';

/**
 * 分页查询应用列表
 * @param params 查询参数
 * @returns 分页结果
 */
export function getAppPage(params: AppQueryParams) {
  return request<PageResult<App>>({
    url: '/api/apps',
    method: 'get',
    params
  });
}

/**
 * 获取应用详情
 * @param id 应用ID
 * @returns 应用详情
 */
export function getAppDetail(id: number) {
  return request<App>({
    url: `/api/apps/${id}`,
    method: 'get'
  });
}

/**
 * 创建应用
 * @param data 应用数据
 * @param icon 应用图标
 * @param appPackage 应用包
 * @param screenshots 应用截图
 * @returns 应用ID
 */
export function createApp(data: AppRequest, icon: File, appPackage: File, screenshots?: File[]) {
  const formData = new FormData();
  formData.append('name', data.name);
  formData.append('description', data.description);
  formData.append('packageName', data.packageName);
  formData.append('version', data.version);
  formData.append('versionCode', data.versionCode.toString());
  data.categoryIds.forEach(categoryId => {
    formData.append('categoryIds', categoryId.toString());
  });

  formData.append('icon', icon);
  formData.append('package', appPackage);

  if (screenshots && screenshots.length > 0) {
    screenshots.forEach(screenshot => {
      formData.append('screenshots', screenshot);
    });
  }

  return request<number>({
    url: '/api/apps',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 更新应用
 * @param data 应用数据
 * @param icon 应用图标
 * @param appPackage 应用包
 * @param screenshots 应用截图
 * @returns 是否成功
 */
export function updateApp(data: AppRequest, icon?: File, appPackage?: File, screenshots?: File[]) {
  const formData = new FormData();
  formData.append('id', data.id!.toString());
  formData.append('name', data.name);
  formData.append('description', data.description);
  formData.append('packageName', data.packageName);
  formData.append('version', data.version);
  formData.append('versionCode', data.versionCode.toString());
  data.categoryIds.forEach(categoryId => {
    formData.append('categoryIds', categoryId.toString());
  });

  if (icon) {
    formData.append('icon', icon);
  }

  if (appPackage) {
    formData.append('package', appPackage);
  }

  if (screenshots && screenshots.length > 0) {
    screenshots.forEach(screenshot => {
      formData.append('screenshots', screenshot);
    });
  }

  return request<boolean>({
    url: '/api/apps',
    method: 'put',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 删除应用
 * @param id 应用ID
 * @returns 是否成功
 */
export function deleteApp(id: number) {
  return request<boolean>({
    url: `/api/apps/${id}`,
    method: 'delete'
  });
}

/**
 * 更新应用状态
 * @param id 应用ID
 * @param status 状态
 * @returns 是否成功
 */
export function updateAppStatus(id: number, status: number) {
  return request<boolean>({
    url: `/api/apps/${id}/status`,
    method: 'put',
    params: { status }
  });
}

/**
 * 提交应用审核
 * @param id 应用ID
 * @returns 是否成功
 */
export function submitToAudit(id: number) {
  return request<boolean>({
    url: `/api/apps/${id}/submit`,
    method: 'post'
  });
}
